package com.xmjs.baselibrary.base.application;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.webkit.WebView;

import androidx.annotation.RequiresApi;
import androidx.multidex.MultiDex;

import com.xmjs.baselibrary.utils.ToastUtil;
import com.xmjs.baselibrary.view.swipeback.ActivityLifecycleHelper;
import com.zxy.tiny.Tiny;

import static com.xmjs.baselibrary.broadcast.AppFrontBackReceiver.APP_BACT_TO_FRONT;
import static com.xmjs.baselibrary.broadcast.AppFrontBackReceiver.APP_FRONT_TO_BACK;

public class LibBaseApplication extends Application implements
        Application.ActivityLifecycleCallbacks {
    //打开的Activity数量统计
    int activityStartCount = 0;
    //上下文
    public static Context context;

    public LibBaseApplication() {
        context = this;
    }

    public static Context getContext() {
        return context;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        //初始化Toast
        ToastUtil.init(this);

        //初始化Tiny图片压缩
        Tiny.getInstance().init(this);

        //监听所有Activity的生命周期
        registerActivityLifecycleCallbacks(this);
        registerActivityLifecycleCallbacks(ActivityLifecycleHelper.build());

        //Android P以及之后版本不支持同时从多个进程使用具有相同数据目录的WebView；
        //因此在初始化的时候，需要为其它进程webView设置目录
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
            webviewSetPath(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //方法数超过最大值，需要分包
        MultiDex.install(this);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityStarted(Activity activity) {
        activityStartCount++;
        //数值从0变到1说明是从后台切到前台
        if (activityStartCount == 1)
            sendBroadcast(new Intent(APP_BACT_TO_FRONT));
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        activityStartCount--;
        //数值从1到0说明是从前台切到后台
        if (activityStartCount == 0)
            sendBroadcast(new Intent(APP_FRONT_TO_BACK));

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }

    /**
     * 为其它进程webView设置目录
     *
     * @param context
     */
    @RequiresApi(api = 28)
    public void webviewSetPath(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName(context);

            if (!getApplicationContext().getPackageName().equals(processName)) {//判断不等于默认进程名称
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

    /**
     * 获取进程名称
     *
     * @param context
     * @return
     */
    public String getProcessName(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == android.os.Process.myPid()) {
                return processInfo.processName;
            }
        }
        return null;
    }

    /**
     * 判断某activity是否处于栈顶
     *
     * @return
     */
    public boolean isActivityTop(Class cls, Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        String name = manager.getRunningTasks(1).get(0).topActivity.getClassName();
        return name.equals(cls.getName());
    }
}
